package com.lcn.netty.initializer;

import com.lcn.netty.handler.HttpHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;

/**
 * 初始化器，channel注册后，会执行里面的相应的初始化方法
 */
public class HttpServerInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        // 通过socket获得对应的管道
        ChannelPipeline pipeline = socketChannel.pipeline();
        // 通过管道添加handler处理器
        pipeline.addLast("HttpServerCodec", new HttpServerCodec());
        // 自定义助手类，访问时返回"hello netty"
        pipeline.addLast("HttpHandler", new HttpHandler());
    }

}
